require 'crawler/crawler'
require 'uri'
require 'date'
require 'rexml/document'
require 'html/xmltree'
require 'utils'

module Crawler
  
  class BlogspotCrawler < Crawler
    
    public
    
    def initialize(options = {})
      super options
      @curl.enable_cookies = true
      @curl.follow_location = true
      @curl.max_redirects = -1
    end
    
    def reset_http_session
      super
      @curl.enable_cookies = true
    end
    
    def crawl_table
      BlogspotCrawl
    end
    
    def update_url_conditios 
      ['ff_id = ? and user_url_on_service like ?', service_name, 'http://%.blogspot.com%']
    end
    
    
    def service_name
      'blog'
    end
    
    protected
    
    def crawl(c)
      #      full_url = URI.parse c.service.user_url_on_service
      #      request_uri = "#{full_url.scheme}://#{full_url.host}/" # per ottenere la homepage del blog
      request_uri = Utils.get_base_uri c.service.user_url_on_service
      @curl.url = request_uri
      @curl.http_get
      
      begin
        parser = HTMLTree::XMLParser.new false, true
        parser.feed @curl.body_str
      rescue REXML::ParseException => e
        # continue with a partial prased document
        p e
      end
      
      # try to detect if blog has move to a different url
#      if @curl.response_code == 301 then #the blog has movede to a different url        
#        new_url = REXML::XPath.match parser.document.root, './body/a[text() = "here"]'
#        if new_url.length == 1 then
#          @curl.url = Utils.get_base_uri new_url[0].attribute('href').value
#          @curl.http_get
#        end
#      end
      
      # now i try to detect if auth i required to access the bblog
      iframe = REXML::XPath.match parser.document.root, '//iframe[@id = "injected-iframe"]'
      if iframe.length == 1 then # auth required
        @curl.url = Utils.get_base_uri iframe[0].attribute('src').value
        @curl.http_get
        if @curl.response_code != 200 then
          c.status = 6969 #this should never be done
        else
          parser.reset
          parser.feed @curl.body_str
          continue_button = REXML::XPath.match parser.document.root, '//a[@id = "continueButton"]'
          if continue_button.length != 1 then
            c.status = 6968 #this should never be done
          else
            @curl.url = continue_button[0].attribute('href').value
            @curl.http_get
            parser.reset
            parser.feed @curl.body_str
            redirec = REXML::XPath.match parser.document.root, '//a[text() = "here"]'
            if redirec.length != 1 then
              c.status = 6967 #this should never be done
            else
              @curl.url = redirec[0].attribute('href').value
              @curl.http_get
            end
          end
        end
      end
      
      response = @curl.response_code == 200
      
      if response then
        blogger_link = REXML::XPath.match parser.document.root, '//link[@rel = "me"]'
        if blogger_link.length == 1 then
          blogger_uri = URI.parse blogger_link[0].attribute('href').value
          blogger_userid = blogger_uri.path.split('/')[2]
          blogger_crawl = BloggerCrawl.find_by_blogger_userid blogger_userid          
          blogger_crawl = BloggerCrawl.create :blogger_userid => blogger_userid if blogger_crawl.nil?
          c.blogger_crawl_id = blogger_crawl.id
        end
      end
      
      c.status = @curl.response_code
      c.body = @curl.body_str
      response
    end
    
    protected
    
    def has_crawl?(s)
      !s.blogspot_crawl.nil?
    end
    
  end
  
end